package edu.neusoft.yiqibike.common.config;

import edu.neusoft.yiqibike.common.constant.GlobalConstant;
import edu.neusoft.yiqibike.common.security.MyShiroRealm;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.session.mgt.DefaultSessionManager;
import org.apache.shiro.session.mgt.SessionManager;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;

import java.util.LinkedHashMap;
import java.util.Map;

//TODO 配置rememberMe、缓存、cookieManager、注解
@Slf4j
@Order(1)
@Configuration
public class ShiroConfiguration {
    @Bean
    public ShiroFilterFactoryBean shiroFilter(SecurityManager securityManager) {
        ShiroFilterFactoryBean factoryBean = new ShiroFilterFactoryBean();
        factoryBean.setSecurityManager(securityManager);
        //设置登录路径,如果不设置默认会自动寻找Web工程根目录下的"/login.jsp"页面 302重定向页面
        factoryBean.setLoginUrl("/error/login");
        //设置登录成功后跳转的路径
//        factoryBean.setSuccessUrl("/index");
        //设置未授权403页面
//        factoryBean.setUnauthorizedUrl("/error/403");
        //配置拦截器链
        Map<String, String> filterChainDefinitionMap = new LinkedHashMap<>();
        //静态资源
        filterChainDefinitionMap.put("/static/**", "anon");
        //退出
        filterChainDefinitionMap.put("/sys/admin/logout", "logout");
//        filterChainDefinitionMap.put("/admin/login", "anon");
        //登录验证
//        filterChainDefinitionMap.put("/**", "authc");
        filterChainDefinitionMap.put("/user/list", "authc");
        filterChainDefinitionMap.put("/sys/ip/list", "authc");
        //权限认证
//        filterChainDefinitionMap.put("/user", "perms[test]");
        factoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
        return factoryBean;
    }

    //配置SecurityManager
    @Bean
    public SecurityManager securityManager() {
        DefaultWebSecurityManager manager = new DefaultWebSecurityManager();
        manager.setRealm(myShiroRealm());
        return manager;
    }

    //自定义realm
    @Bean
    public MyShiroRealm myShiroRealm() {
        MyShiroRealm shiroRealm = new MyShiroRealm();
        shiroRealm.setCredentialsMatcher(hashedCredentialsMatcher());
        return shiroRealm;
    }

    //配置md5加密
    @Bean
    public HashedCredentialsMatcher hashedCredentialsMatcher() {
        HashedCredentialsMatcher credentialsMatcher = new HashedCredentialsMatcher();
        //设置加密算法
        credentialsMatcher.setHashAlgorithmName(Md5Hash.ALGORITHM_NAME);
        //设置散列次数，比如散列两次，相当于md5(md5(密码))
        credentialsMatcher.setHashIterations(GlobalConstant.HASH_ITERATIONS);
        //表示是否存储散列后的密码为16进制，需要和生成密码时的一样，默认是base64
        credentialsMatcher.setStoredCredentialsHexEncoded(true);
        return credentialsMatcher;
    }

    //开启shiro aop注解
    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager) {
        AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
        authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
        return authorizationAttributeSourceAdvisor;
    }
}
